package com.freewater.javabase.demo.network.uri;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class QueryString {

    private String key;

    private String value;

    public QueryString(String key, String value) {
        if (key == null || key.isBlank()) {
            throw new IllegalArgumentException("key is null or empty");
        }
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString() {
        return key.trim() + "=" + (value == null ? "" : URLEncoder.encode(value, StandardCharsets.UTF_8));
    }

    public static void main(String[] args) {
        QueryString queryString = new QueryString("key1", "值1");
        System.out.println(queryString);
        queryString = new QueryString("key2", null);
        System.out.println(queryString);
    }
}
